#ifndef _DISPLAY_H_
#define _DISPLAY_H_
#include <ESP8266WiFi.h>
int flag_wifi;
int key1 = 5;      //向上选择按键
int key2 = 2;      //向下选择按键
int key3 = 4;      //确定按键    
int Key_Exit = 0; //退出按键 
int LED = 13;      //照明灯
int analogPin = A0;//环境亮度采集
int Out = 12;//户外模式开灯标记位
 
 int Mode_i;  //模式选择指针0
 int Light_i; //调光模式0
 int Clock_i; //定时时长0
 int Clock_flag;//定时模式指针0
 int flag;    //定时标记位0


void Mode_Display();//显示模式。。主页面
void Mode_One();     //模式一
void  Mode_two();     //模式二
 void Mode_three();  //模式三
void Mode_four();    //模式四

/*---------------模式选择display----------------*/
 void Mode_Display(){
  OLED_Clear();
digitalWrite(LED, 0);
  Mode_i=0;
      OLED_ShowCHinese(0, 0, 1);
   for(int tm = 0; tm<4; tm++)
     OLED_ShowCHinese(32+tm*16, 0, 2+tm);//室外模式
   for(int tm = 0; tm<4; tm++)
     OLED_ShowCHinese(32+tm*16, 2, 6+tm);//室内模式
   for(int tm = 0; tm<4; tm++)
     OLED_ShowCHinese(32+tm*16, 4, 10+tm);//定时模式
   for(int tm = 0; tm<4; tm++)
     OLED_ShowCHinese(32+tm*16, 6, 14+tm);//蹦迪模式
     flag_wifi = 0;
}

 
   /*--------------模式一--------------*/
  void Mode_One(){
     for(int tm = 0; tm<4; tm++)
       OLED_ShowCHinese(32+tm*16, 0, 2+tm);//室外模式
     for(int tm = 0; tm<8; tm++)
       OLED_ShowCHinese(tm*16, 2, 18+tm);//检测到人进入视野
      for(int tm = 0; tm<6; tm++)
       OLED_ShowCHinese(tm*16, 4, 26+tm);//时,自动亮灯  
      Out = 0;  
     while(digitalRead(Key_Exit)){
    //  Serial.println(digitalRead(Out_Mode_PIN)); 
        delay(10);
        if(digitalRead(Out))
             digitalWrite(LED, 1); 
         else  
           digitalWrite(LED, 0);          
     }
     Mode_Display();
    }
   /*--------------模式二--------------*/ 
  void  Mode_two(){
    for(int tm = 0; tm<4; tm++)     
     OLED_ShowCHinese(32+tm*16, 0, 6+tm);//室内模式
     for(int tm = 0; tm<8; tm++)     
     OLED_ShowCHinese(tm*16, 2, 32+tm);//根据室内亮度自动
    for(int tm = 0; tm<8; tm++)     
     OLED_ShowCHinese(tm*16, 4, 40+tm);//调光,也可以手动
    for(int tm = 0; tm<2; tm++)     
     OLED_ShowCHinese(tm*16, 6, 48+tm);//调光 

     while(digitalRead(Key_Exit)){//死循环，直到退出
        if(!digitalRead(key3)){
          delay(80);
         if(!digitalRead(key3)){
           Light_i++;
           if(Light_i==4) Light_i = 0;
            switch (Light_i){
              case 0:      for(int tm = 0; tm<4; tm++)     
                             OLED_ShowCHinese(16*4+tm*16, 6, 67+tm);//亮度一档
                             analogWrite(LED, 300);
                             flag = 1; //不进入自动调光
                           break;
              case 1:     for(int tm = 0; tm<4; tm++)     
                             OLED_ShowCHinese(16*4+tm*16, 6, 71+tm);//亮度二档 
                             analogWrite(LED, 600);
                             flag = 1; //不进入自动调光
                           break;
              case 2:    for(int tm = 0; tm<4; tm++)     
                             OLED_ShowCHinese(16*4+tm*16, 6, 75+tm);//亮度三档 
                             analogWrite(LED, 1023);
                             flag = 1; //不进入自动调光
                           break;
              case 3:    flag = 0; //进入自动调光 
                          for(int tm = 0; tm<4; tm++)     
                            OLED_ShowCHinese(16*4+tm*16, 6, 79+tm);//自动调光
                           break;
              default: break;
            }
         }
       }
         if(!flag){//自动调光

                analogWrite(LED , analogRead(analogPin));
         }
         delay(1);
     }
     Mode_Display();
  }
   /*--------------模式三--------------*/
  void Mode_three(){
    for(int tm = 0; tm<4; tm++)
      OLED_ShowCHinese(32+tm*16, 0, 10+tm);//定时模式
    for(int tm = 0; tm<4; tm++)
      OLED_ShowCHinese(16+tm*16, 2, 50+tm);//延时打开
    for(int tm = 0; tm<4; tm++)
      OLED_ShowCHinese(16+tm*16, 4, 54+tm);//延时关闭   
    for(int tm = 0; tm<3; tm++)
      OLED_ShowCHinese(16+tm*16, 6, 58+tm);//定时： 
    for(int tm = 0; tm<2; tm++) 
      OLED_ShowCHinese(16*5+tm*16, 6, 61+tm);//分钟
      delay(300);//防止模式选择按钮跳过定时模式选择
/*-------------------选择定时模式-------------------*/
   OLED_ShowCHinese(0, 2, 83);//默认模式
         Clock_flag = 2;
   while(digitalRead(key3)){//选择
      delay(1);
    if(!digitalRead(key1) || !digitalRead(key2)){
     if(!digitalRead(key2)){
    for(int cl=0; cl<4; cl++)  //清指针
       OLED_ShowCHinese(0, cl*2, 0);
       
         OLED_ShowCHinese(0, 2, 83);
         Clock_flag = 2;
       }
     if(!digitalRead(key1)){
     for(int cl=0; cl<4; cl++)  //清指针
       OLED_ShowCHinese(0, cl*2, 0);
       
         OLED_ShowCHinese(0, 4, 83);
         Clock_flag = 4;
     }   
   }
 }delay(300);//防止定时模式选择按钮跳过定时时间选择
 /*------------------定时时间------------------------*/
 Clock_i = 22;
   OLED_ShowChar(4*16, 6, Clock_i/10);
   OLED_ShowChar(4*16+8, 6, Clock_i%10);
 while(digitalRead(key3)){
         delay(1);
   if(!digitalRead(key1) || !digitalRead(key2)){//选择
      delay(50);
       if(!digitalRead(key1) || !digitalRead(key2)){
     if(!digitalRead(key2)){
         Clock_i++;
         if(Clock_i==99) Clock_i=0;
       }
     if(!digitalRead(key1)){
         Clock_i--;
         if(Clock_i==-1) Clock_i=99;
     }   
   }
   OLED_ShowChar(4*16, 6, Clock_i/10);
   OLED_ShowChar(4*16+8, 6, Clock_i%10);
   }
 }  
/*-------------------------------------------*/   
         flag = 1;//表图案动 
       if(Clock_flag == 2) digitalWrite(LED, 0);
       else  digitalWrite(LED, 1); 
          
      while(digitalRead(Key_Exit)){
         delay(1);//防跑飞
        for(int tm=0; tm<4&&flag; tm++){//表图案动
         delay(250);
          OLED_ShowCHinese(0 , Clock_flag, 83+tm);
        } 
        
       OLED_ShowChar(4*16, 6, Clock_i/10);
       OLED_ShowChar(4*16+8, 6, Clock_i%10); 
       
       if(Clock_i == 0){
          if(Clock_flag == 2) digitalWrite(LED, 1);
          else  digitalWrite(LED, 0);;
          flag = 0;
       }
       else if(Clock_i > 0)
         Clock_i--;
      }
      Mode_Display();
    }
   /*--------------模式四--------------*/
  void Mode_four(){
    for(int tm = 0; tm<4; tm++)
      OLED_ShowCHinese(32+tm*16, 0, 14+tm); 
   for(int tm = 0; tm<4; tm++)
      OLED_ShowCHinese(32+tm*16, 4, 63+tm); 
      while(digitalRead(Key_Exit)){
        digitalWrite(LED, 1);
          delay(240);          
        digitalWrite(LED, 0);
          delay(240);
      }
      Mode_Display();
  }
#endif _DISPLAY_H_
